package com.ljb.calendar.week;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

import com.ljb.calendar.DateViewAdapter;
import com.ljb.calendar.OnDateViewClickListener;
import com.ljb.calendar.OnPageSelectedListener;
import com.ljb.calendar.month.MonthCalendarView;
import com.ljb.calendar.month.MonthView;

import org.joda.time.LocalDate;

/**
 * 单行日历控件
 * Created by ljb on 2018/3/21.
 */

public class WeekCalendarView extends ViewPager {
    private static final int HALF_INTEGER_MAX_VASLUE = Integer.MAX_VALUE / 2;
    private SparseArray<WeekView> mWeekViews = new SparseArray<>();
    private DateViewAdapter mDateViewAdapter;
    private OnDateViewClickListener mOnDateViewClickListener;
    private OnPageSelectedListener mOnPageSelectedListener;
    private LocalDate mTodayDate;
    private LocalDate mStartDate;

    public WeekCalendarView(Context context) {
        this(context, null);
    }

    public WeekCalendarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setWillNotDraw(true);
        initStartDate();
        setAdapter(mPagerAdapter);
        addOnPageChangeListener(mOnPageChangeListener);
        setCurrentItem(HALF_INTEGER_MAX_VASLUE, false);
    }

    private void initStartDate() {
        mTodayDate = LocalDate.now();
        mStartDate = mTodayDate.plusDays(-mTodayDate.getDayOfWeek());
    }

    public void setOnDateViewClickListener(OnDateViewClickListener onDateViewClickListener) {
        mOnDateViewClickListener = onDateViewClickListener;
    }

    public void setDateViewAdapter(DateViewAdapter dateViewAdapter) {
        mDateViewAdapter = dateViewAdapter;
    }

    public void setOnPageSelected(OnPageSelectedListener onPageSelectedListener) {
        mOnPageSelectedListener = onPageSelectedListener;
    }

    private PagerAdapter mPagerAdapter = new PagerAdapter() {

        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            mWeekViews.remove(position);
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            if (mWeekViews.get(position) == null) {
                WeekView weekView = new WeekView(getContext());
                weekView.setDateViewAdapter(mDateViewAdapter);
                weekView.setOnDateViewClickListener(mOnDateViewClickListener);
                weekView.setData(mTodayDate, mStartDate.plusWeeks(position - HALF_INTEGER_MAX_VASLUE), position);
                mWeekViews.put(position, weekView);
            }
            container.addView(mWeekViews.get(position));
            return mWeekViews.get(position);
        }
    };

    private OnPageChangeListener mOnPageChangeListener = new SimpleOnPageChangeListener() {

        @Override
        public void onPageSelected(final int position) {
            WeekView weekView = mWeekViews.get(position);
            if (weekView != null) {
                if (mOnPageSelectedListener != null)
                    mOnPageSelectedListener.onPageSelected(weekView, position);
            } else {
                WeekCalendarView.this.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        onPageSelected(position);
                    }
                }, 50);
            }
        }
    };
}
